package jr.controller;

import com.google.gson.Gson;
import jr.pojo.Users;
import jr.service.IUserService;
import jr.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


@WebServlet("/login/*")
public class UsersController extends HttpServlet {

    private IUserService ius=new UserServiceImpl();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        int index = requestURI.lastIndexOf("/") + 1;
        String substring = requestURI.substring(index);
        if (substring.length()==0){
            substring="showdt";
        }

        Class empControllerClass = this.getClass();
        Method method=null;

        try {
            method=empControllerClass.getMethod(substring,HttpServletRequest.class,HttpServletResponse.class);

            response.setContentType("application/json;charset=utf-8");
            request.setCharacterEncoding("utf-8");


            method.invoke(this,request,response);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }


    public void showdt (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String user = request.getParameter("user");
        String rag="^[^'|&]*$";
        if (user.length()>4&&user.matches(rag)){
            String psd = request.getParameter("psd");
            if (psd.length()>4&&psd.matches(rag)){
                boolean b=ius.queryUsers(user,psd);
                response.getWriter().println(new Gson().toJson(b));
            }else {
                System.out.println("错误");
            }
        }else {
            System.out.println("错误");
        }
    }



    }
